package com.baizhi.springboot.service.impl;

import com.baizhi.springboot.annotation.LogAnnotation;
import com.baizhi.springboot.dao.AlbumDao;
import com.baizhi.springboot.dao.AudioDao;
import com.baizhi.springboot.entity.Album;
import com.baizhi.springboot.entity.AlbumDTO;
import com.baizhi.springboot.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    private AlbumDao albumDao;
    @Autowired
    private AudioDao audioDao;

    @Override
    @LogAnnotation(content = "查询所有专辑分页",type = "查询操作")
    //@Transactional(readOnly = false)
    public Map selectAlbumByPage(Integer page, Integer limit) {
        Integer offset=(page-1)*limit;
        List<Album> albums = albumDao.selectAlbumByPage(offset, limit);
        Integer count = albumDao.selectCount();
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", albums);
        return map;
    }

    @Override
    @LogAnnotation(content = "添加专辑",type = "添加操作")
    public void insertAlbum(Album album) {
        albumDao.insertAlbum(album);
    }

    @Override
    @LogAnnotation(content = "删除专辑",type = "删除操作")
    public void deleteAlbum(Integer id) {
        audioDao.deleteByAlbumAudio(id);
        albumDao.deleteAlbum(id);
    }

    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询单个专辑",type = "查询操作")
    public Album selectOneAlbum(Integer id) {
        return albumDao.selectOneAlbum(id);
    }

    @Override
    @LogAnnotation(content = "修改专辑",type = "修改操作")
    public void updateAlbum(Album album) {
        albumDao.updateAlbum(album);
    }

    @Override
    //@Transactional(readOnly = true)
    @LogAnnotation(content = "查询所有专辑",type = "查询操作")
    public List<Album> selectAllAlbum() {
        return albumDao.selectAllAlbum();
    }

    @Override
    @LogAnnotation(content = "查询专辑饼状图",type = "查询操作")
    public List<Map> getAudioNumber() {
        return albumDao.getAudioNumber();
    }

    @Override
    @LogAnnotation(content = "查询专辑柱状图",type = "查询操作")
    public List<AlbumDTO> getAudioNumberList() {
        return albumDao.getAudioNumberList();
    }
}
